package cn.zwx.distributed.svc.controller;

import cn.zwx.base.entity.ResponseEntity;
import cn.zwx.base.exception.BizWrapException;
import cn.zwx.distributed.svc.service.UidGeneratorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangwenxue
 */
@RestController
@Api(tags = "分布式id相关接口")
@RequestMapping("/distributed")
public class DistributedController  {
    @Resource(name="cachedUidGeneratorServiceImpl")
    private UidGeneratorService cachedUidGeneratorService;


    @ApiOperation(value = "获取分布式Id接口")
    @RequestMapping(value = "/get",method = RequestMethod.POST)
    public ResponseEntity<String> getDistributedId() throws BizWrapException {
        long distributedId = cachedUidGeneratorService.getDistributedId();
        return ResponseEntity.success(String.valueOf(distributedId));
    }

    @ApiOperation(value = "解析全局分布式ID接口")
    @RequestMapping(value = "/parse",method = RequestMethod.POST)
    public ResponseEntity<String> parsDistributedId(@RequestParam(value = "全局id") @NotBlank(message = "全局id不能为空")
                                                        String distributedId) throws BizWrapException {
        String parseId = cachedUidGeneratorService.parsDistributedId(Long.parseLong(distributedId));
        return ResponseEntity.success(parseId);
    }
}
